//
//  PersonalInfoDetailViewController.m
//  DriftDog
//
//  Created by rimi on 15/9/9.
//  Copyright (c) 2015年 廖马林. All rights reserved.
//

#import "PersonalInfoDetailViewController.h"

enum {
    
    infoTag = 100
};

@interface PersonalInfoDetailViewController () <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    NSMutableDictionary *_dataSource;  // 用来存储请求下来的资源，姓名啊，什么的。
}

@property (nonatomic, strong) UIActionSheet *mySheet;  // 选择的下面弹出框

@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, strong) UIView *headImageViewBack;
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UITapGestureRecognizer *gesture;  // 全局手势，修改

- (void)initializeUserInterface;

@end

@implementation PersonalInfoDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}

#pragma mark - init

- (void)initializeDataSource {

    self.titleArray = @[@"昵称", @"性别", @"坐标", @"个性签名"];
    
}

- (void)initializeUserInterface {
    
    self.view.backgroundColor = [UIColor colorWithWhite:236 / 255.f alpha:1];

    /* 添加父类控件 */
    [self addBaseLeftButtonWithImage:[UIImage imageNamed:@"返回图标"]];
    [self.baseLeftButton addTarget:self action:@selector(respondsToBaseLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addTitleLabelWithName:@"个人资料"];
    [self addBaseRightButtonWithImage:[UIImage imageNamed:@"提交.png"]];
    [self.baseRightButton addTarget:self action:@selector(respondsToBaseRightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    /* 添加子类控件 */
    [self.view addSubview:self.headImageViewBack];
    [self.view addSubview:self.headImage];
    [self.view addSubview:self.nameLabel];
    
    // 添加4个底部白色视图， 和4个标题
    for (int i = 0; i < 4; i ++) {
        
        /* 4个view */
        UIView *view = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 75 + 110 + i * (75 +1), ORIGIN_WIDTH, 75), NO)];
        view.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:view];
        
        /* 4个titleLabel */
        UILabel *label = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 100, 45), NO)];
        label.center = CGPointMake(DHFlexibleVerticalMutiplier() * 70, view.center.y);
        label.text = self.titleArray[i];
        label.textColor = [UIColor colorWithWhite:90 / 255.f alpha:1];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:17 * DHFlexibleVerticalMutiplier()];
        
        [self.view addSubview:label];
        
        /* 4个详情Label */
        UILabel *detailLabel = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 100, 45), NO)];
        detailLabel.center = CGPointMake(DHFlexibleVerticalMutiplier() * 330, view.center.y);
        detailLabel.text = self.titleArray[i];
        detailLabel.textColor = MAINCOLOR;
        detailLabel.alpha = 0.7;
        detailLabel.textAlignment = NSTextAlignmentCenter;
        detailLabel.font = [UIFont systemFontOfSize:16 * DHFlexibleVerticalMutiplier()];
        detailLabel.tag = infoTag + 1 + i;
        
        [self.view addSubview:detailLabel];
        
    }
    
    /* 添加全局手势 */
    [self.view addGestureRecognizer:self.gesture];
}

#pragma mark - responds event

- (void)respondsToBaseLeftButton:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

// 提交信息
- (void)respondsToBaseRightButton:(UIButton *)sender {

    
}

- (void)respondsToTapGesture:(UITapGestureRecognizer *)sender {

    CGPoint location = [sender locationInView:self.view];
    
    if ([self.headImage.layer.presentationLayer hitTest:location]) {  // ---> 1.更改头像
 
        [self.mySheet showInView:self.view];  // 显示出来
    }else if ([[self.view viewWithTag:101].layer.presentationLayer hitTest:location]) { // ---> 2.更改昵称
    
    
    }else if ([[self.view viewWithTag:102].layer.presentationLayer hitTest:location]) { // ---> 3.更改性别
    
    
    }else if ([[self.view viewWithTag:103].layer.presentationLayer hitTest:location]) { // ---> 4.更改坐标
    
    
    }else if ([[self.view viewWithTag:104].layer.presentationLayer hitTest:location]) { // ---> 5.更改个性签名
    
        
    }
    
}

#pragma mark - <UIActionSheetDelegate>

// 点击了actionSheet的某一个按钮
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == self.mySheet.cancelButtonIndex) {
    
        // 取消操作
    }
    
    switch (buttonIndex) {
        case 0:   // ---> 1.打开照相机
            [self takePhoto];
            break;
        case 1:   // ---> 2.打开本地相册
            [self openLocalPhotos];
            break;
            
        default:
            break;
    }
}


#pragma mark - <UIImagePickerControllerDelegate>

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
#warning 这儿的问题 public.image
    // 当选择的类型是图片
    if ([type isEqualToString:@"public.image"]) {
        
        // 先把图片转成NSData
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil) {
            
            data = UIImageJPEGRepresentation(image, 1.0);
        }else {
        
            data = UIImagePNGRepresentation(image);
        }
#warning 这里来做操作，提交的时候要上传
        // 图片保存的路径
       
    }
}

#pragma mark - private methods

// 打开照相机
- (void)takePhoto {

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        // 设置拍照后的图片可以被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];
    }else {
    
        NSLog(@"不能打开照相机，请打开真机调试");
    }
}

// 打开本地相册
- (void)openLocalPhotos {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    // 设置选择后的图片可以被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}


#pragma mark - getter

- (UIView *)headImageViewBack {

    if (!_headImageViewBack) {
        
        _headImageViewBack = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 75, ORIGIN_WIDTH, 100), NO)];
        _headImageViewBack.backgroundColor = [UIColor whiteColor];
        
    }
    return _headImageViewBack;
}

- (UIImageView *)headImage {

    if (!_headImage) {
        
        _headImage = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 70, 70), NO)];
        _headImage.center = DHFlexibleCenter(CGPointMake(330, 75 + 50));
        _headImage.backgroundColor = [UIColor orangeColor];
        _headImage.layer.cornerRadius = self.headImage.bounds.size.height / 2;
        _headImage.layer.masksToBounds = YES;
        _headImage.tag = infoTag;  // tag = 100
    }
    return _headImage;
}

- (UILabel *)nameLabel {

    if (!_nameLabel) {
        
        _nameLabel = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(30, 75 + 50, 100, 45), NO)];
        _nameLabel.center = DHFlexibleCenter(CGPointMake(70, 75 + 50));
        _nameLabel.text = @"个人头像";
        _nameLabel.textColor = [UIColor colorWithWhite:90 / 255.f alpha:1];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.font = [UIFont systemFontOfSize:17 * DHFlexibleVerticalMutiplier()];
    }
    return _nameLabel;
}

- (UITapGestureRecognizer *)gesture {

    if (!_gesture) {
        
        _gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToTapGesture:)];
    }
    return _gesture;
}

- (UIActionSheet *)mySheet {
#warning 这儿设置为取消为红色，怎么做。
    if (!_mySheet) {

        _mySheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
            }
    return _mySheet;
}

@end
